import java.util.Arrays;

// 二进制求和
//https://leetcode.cn/problems/add-binary/description/
public class Test {
    public static void main(String[] args) {
        //
        /*String s1 = "aaa";
        String s2 = "bbb";
        s1 = s1+s2;
        System.out.println(s1);
        int[] nums = {1,2,3,4,5,6,7,8,9};
        //System.out.println(Arrays.stream(nums).toArray());
        //Arrays.(nums);

        int[] intArray = {1, 2, 3, 4, 5};
        String result = Arrays.toString(intArray);
        System.out.println(result);*/
        String a = "1";
        String b = "111";
        Solution s = new Solution();
        String res = s.addBinary(a,b);
        System.out.println(res);

    }
}


class Solution {
    public String addBinary(String a, String b) {
        //
        String res = "";
        if (a.length() > b.length()) {
            String tmp = "";
            for (int i = 0; i < a.length()-b.length(); i++) {
                tmp +="0";
            }
            b = tmp + b;
        }else if (a.length() < b.length()) {
            String tmp = "";
            for (int i = 0; i < b.length()-a.length(); i++) {
                tmp +="0";
            }
            a = tmp + a;
        }


        int n = a.length();
        int tmp = 0;
        int up = 0;
        int stay = 0;
        for(int i = n-1; i >= 0; i--){
            tmp = a.charAt(i) + b.charAt(i);
            stay = (tmp-'0' + up-'0') % 2;//这一位的结果
            up = (tmp-'0' + up-'0') / 2;//进位
            res = stay + res;

        }
        if(up != 0){
            res = "1"+res;
        }
        return res;
    }
}
//class Solution {
//    @Override
//    public String toString() {
//        return super.toString();
//    }
//
//    public String addBinary(String a, String b) {
//        //
//        String res = "";
//        if (a.length() > b.length()) {
//            String tmp = "";
//            for (int i = 0; i < a.length()-b.length(); i++) {
//                tmp +="0";
//            }
//            b = tmp + b;
//        }else if (a.length() < b.length()) {
//            String tmp = null;
//            for (int i = 0; i < b.length()-a.length(); i++) {
//                tmp +="0";
//            }
//            a = tmp + a;
//        }
//
//
//        int n = a.length();
//        int[] resArr = new int[n+1];
//        int tmp = 0;
//        int up = 0;
//        int stay = 0;
//        for(int i = n-1; i >= 0; i--){
//
//            tmp = a.charAt(i) + b.charAt(i);
//            stay = (tmp + up) % 2;//这一位的结果
//            resArr[i] = a.charAt(i)-'0';//
//            up = (tmp + up) / 2;//进位
//            res = stay + res;
//
//        }
//        if(up != 0){
//            resArr[0] = up;
//            return resArr.toString();
//        }
//        return res.toString().substring(1,n-1);
//    }
//}